/*
 *  Copyright 2012 Dirk Vranckaert
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package eu.vranckaert.vvl.utils.context;

import android.content.Context;
import android.util.Log;

import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

/**
 * User: DIRK VRANCKAERT
 * Date: 28/02/12
 * Time: 11:28
 */
public class DateUtil {
    private static final String LOG_TAG = DateUtil.class.getSimpleName();

    /**
     * Contains all system specific methods.
     */
    public static class System {
        /**
         * Find out if a 24 hours clock is preferred or not. The check will be done based on the user's locale.
         * @param context The context to find the uers's locale.
         * @return {@link Boolean#TRUE} if the 24 hours format is preferred. {@link Boolean#FALSE} if the AM/PM notation
         * is preferred.
         */
        public static boolean is24HourClock(Context context) {
            Locale locale = ContextUtil.getCurrentLocale(context);
            return is24HourClock(locale);
        }

        /**
         * Find out, based on the user's locale, if a 24 hours clock is preferred or not.
         * @param locale The user's locale.
         * @return {@link Boolean#TRUE} if the 24 hours format is preferred. {@link Boolean#FALSE} if the AM/PM notation
         * is preferred.
         */
        public static boolean is24HourClock(Locale locale) {
            java.text.DateFormat dateFormat = java.text.DateFormat.getTimeInstance(java.text.DateFormat.FULL, locale);
            String t = dateFormat.format(new Date());

            java.text.DateFormat stdFormat = java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT, Locale.US);
            java.text.DateFormat localeFormat = java.text.DateFormat.getTimeInstance(java.text.DateFormat.LONG, locale);
            String check = "";
            try {
                check = localeFormat.format(stdFormat.parse("7:00 PM"));
            } catch (ParseException ignore) {
                return false;
            }
            boolean is24HourClock = check.contains("19");

            Log.d(LOG_TAG, "Result of is24HourClock: " + is24HourClock);

            return is24HourClock;
        }
    }
}
